<template>
  <div class="login-container">
    <div class="login-background">
      <div class="login-form-container">
        <el-card class="login-card" shadow="hover">
          <div class="login-header">
            <el-icon class="logo-icon"><Platform /></el-icon>
            <h2 class="login-title">{{ $t('login') }}</h2>
            <p class="login-subtitle">欢迎使用后台管理系统</p>
          </div>
          
          <el-form :model="form" @submit.prevent="onLogin" class="login-form">
            <el-form-item>
              <el-input 
                v-model="form.userName" 
                placeholder="请输入用户名"
                size="large"
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item>
              <el-input 
                @keydown.enter="onLogin"
                v-model="form.password" 
                type="password" 
                placeholder="请输入密码"
                size="large"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            <el-form-item>
              <el-button 
                type="primary" 
                @click="onLogin"
                @keydown.enter="onLogin"
                size="large"
                class="login-button"
                :loading="loading"
              >
                {{ loading ? '登录中...' : $t('login') }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="login-footer">
            <p class="demo-tip">演示账号：用户名admin密码admin</p>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElLoading } from 'element-plus'
import { User, Lock, Platform } from '@element-plus/icons-vue'
import { login } from '@/api/core'
import { useUserStore } from '@/store/user'

const router = useRouter()
const userStore = useUserStore()
const form = ref({ userName: '', password: '' })
const loading = ref(false)

let loadingInstance = null

// 显示全局加载
function showLoading(text = '加载中...') {
  if (loadingInstance) {
    loadingInstance.close()
  }
  
  loadingInstance = ElLoading.service({
    lock: true,
    text: text,
    background: 'rgba(0, 0, 0, 0.7)',
    customClass: 'global-loading'
  })
  
  return loadingInstance
}

// 隐藏全局加载
function hideLoading() {
  if (loadingInstance) {
    loadingInstance.close()
    loadingInstance = null
  }
}

// 设置加载文本
function setLoadingText(text) {
  if (loadingInstance) {
    loadingInstance.setText(text)
  }
}

async function onLogin() {
  if (!form.value.userName || !form.value.password) {
    ElMessage.error('请输入用户名和密码')
    return
  }
  
  loading.value = true
  showLoading('正在登录...')
  try {
    // 调用登录 API
    const response = await login(form.value)
    setLoadingText('登录成功，正在获取用户信息...')
    // 保存 accessToken 到 store
    userStore.setToken(response.accessToken)
    
    router.push({ path: '/' })
  } catch (error) {
    ElMessage.error(error)
    hideLoading()
  } finally {
    loading.value = false
    hideLoading()
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
}

.login-background {
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="white" opacity="0.1"/><circle cx="10" cy="60" r="0.5" fill="white" opacity="0.1"/><circle cx="90" cy="40" r="0.5" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  pointer-events: none;
}

.login-form-container {
  position: relative;
  z-index: 1;
}

.login-card {
  width: 400px;
  border-radius: 16px;
  border: none;
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 16px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin: 16px 0 8px;
}

.login-subtitle {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #409eff 0%, #36a3f7 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.login-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.demo-tip {
  color: #909399;
  font-size: 12px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    width: 90%;
    margin: 0 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style> 